package com.example.hotel.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.hotel.service.*;
import com.example.hotel.dto.*;

@RestController
@RequestMapping("/api")
//@CrossOrigin(origins = "*")
public class hotelController {
    @Autowired
    private hotelService hotelService;

    // 办理入住
    @PostMapping("/checkin")
    public ResponseEntity<Response> checkIn(@RequestBody checkIn checkInRequest) {
        boolean success = hotelService.checkIn(checkInRequest);
        if (success) {
            return ResponseEntity.ok(new Response(true, "办理入住成功"));
        } else {
            return ResponseEntity.ok(new Response(false, "房间已被占用"));
            // return ResponseEntity.status(400).body(new Response(false, "房间已被占用"));
        }
    }

    // 退房
    @GetMapping("/checkout")
    //public ResponseEntity<Response> checkOut(@RequestBody checkOut checkOutRequest) {
        //boolean success = hotelService.checkOut(checkOutRequest);
    public ResponseEntity<Response> checkOut(@RequestParam("roomNumber") int roomNumber) {
        boolean success = hotelService.checkOut(roomNumber);
        if (success) {
            return ResponseEntity.ok(new Response(true, "退房成功"));
        } else {
            return ResponseEntity.ok(new Response(false, "房间未入住或状态错误"));
            // return ResponseEntity.status(400).body(new Response(false, "房间未入住或状态错误"));
        }
    }

    // 打扫房间
    @PostMapping("/clean")
    public ResponseEntity<Response> markRoomClean(@RequestBody clean checkInRequest) {
        boolean success = hotelService.markRoomClean(checkInRequest.getRoomNumber(), checkInRequest.isClean());
        if (success) {
            return ResponseEntity.ok(new Response(true, "打扫状态更新成功"));
        } else {
            return ResponseEntity.ok(new Response(false, "房间不存在或无法更新"));
            // return ResponseEntity.status(400).body(new Response(false, "房间不存在或无法更新"));
        }
    }

    // 延迟退房
    @GetMapping("/extend")
    //public ResponseEntity<Response> extendCheckOut(@RequestBody extend extendRequest) {
    public ResponseEntity<Response> extendCheckOut(@RequestParam("roomNumber") int roomNumber, @RequestParam("newEndTime") String newEndTime) {
        //boolean success = hotelService.extendCheckOut(extendRequest.getRoomNumber(), extendRequest.getNewEndTime());
        boolean success = hotelService.extendCheckOut(roomNumber, newEndTime);
        if (success) {
            return ResponseEntity.ok(new Response(true, "延迟退房成功"));
        } else {
            return ResponseEntity.ok(new Response(false, "延迟退房失败"));
            // return ResponseEntity.status(400).body(new Response(false, "延迟退房失败"));
        }
    }

    //获取WIFI密码
    @GetMapping("/wifiPw")
    public ResponseEntity<Response> getWifiPassword(@RequestParam("roomNumber") int roomNumber){
        Room room=hotelService.getWifiPassWord(roomNumber);
        if(room!=null){
            return ResponseEntity.ok(new Response(true, room.getWifiPassword()));
        }else{
            return ResponseEntity.ok(new Response(true, "房间不存在或wifi无法实验"));
            // return ResponseEntity.status(400).body(new Response(false, "房间不存在或WIFI无法使用"));
        }
    }
}
